package com.aizhixin.quick.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * redis配置
 * @author: hui.zhang
 * @date: 2021/1/21 10:16 上午
 **/

@Configuration
public class RedisConfig {

    /**
     * 默认情况下的模板只能支持RedisTemplate<String,String> 只能存入字符串，
     * 很多时候，我们需要自定义RedisTemplate 设置序列化器 这样我们可以很方便的操作实例对象
     * https://blog.csdn.net/zzhongcy/article/details/102584028
     * @param connectionFactory
     * @return
     */
    @Bean("redisTemplate")
    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}
